﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Internal/Internal.Master" AutoEventWireup="true"
    Theme="Internal" CodeBehind="MachineCategory.aspx.cs" Inherits="Landscape.WebApp.Internal.Admin.MachineCategory" %>

<%@ Register Src="../../Controls/TagItControl.ascx" TagName="TagItControl" TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <telerik:RadToolBar ID="rtbModuleActions" runat="server" Width="100%" EnableShadows="True"
        OnClientButtonClicked="rtbModuleActions_OnClientButtonClicked">
        <Items>
            <telerik:RadToolBarButton runat="server" CommandName="LIST" ImageUrl="~/Images/Search.png"
                Text="Search" CheckOnClick="True" ImagePosition="AboveText" Value="SEARCH">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="NEW" ImageUrl="~/Images/Write.png"
                Text="New" CheckOnClick="True" ImagePosition="AboveText"  Value="NEW">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="SAVE" ImageUrl="~/Images/Floppy.png"
                Text="Save" CheckOnClick="True" ImagePosition="AboveText" Value="SAVE">
            </telerik:RadToolBarButton>
            <telerik:RadToolBarButton runat="server" CommandName="DELETE" ImageUrl="~/Images/Delete_32x32.png"
                Text="Delete" CheckOnClick="True" ImagePosition="AboveText" Value="DELETE">
            </telerik:RadToolBarButton>
        </Items>
    </telerik:RadToolBar>
    <telerik:RadTabStrip ID="rtsSystemManagement" runat="server" MultiPageID="RadMultiPage1"
        SelectedIndex="1" Width="100%">
        <Tabs>
            <telerik:RadTab runat="server" Text="Search Results" PageViewID="RadPageView1">
            </telerik:RadTab>
            <telerik:RadTab runat="server" Text="Details" PageViewID="RadPageView2" 
                Selected="True">
            </telerik:RadTab>
            <telerik:RadTab runat="server" Text="Constituents" PageViewID="RadPageView3">
            </telerik:RadTab>
        </Tabs>
    </telerik:RadTabStrip>
    <telerik:RadMultiPage ID="RadMultiPage1" runat="server" SelectedIndex="1">
        <telerik:RadPageView ID="RadPageView1" runat="server" Selected="true">
            <div class="ContentTabBody">
                <table>
                    <tr>
                        <td>
                            <asp:Label ID="Label9" runat="server" Text="Name :" SkinID="ContentLabel"></asp:Label>
                        </td>
                        <td>
                            <telerik:RadTextBox ID="rtbxNameSearch" runat="server">
                            </telerik:RadTextBox>
                        </td>
                        <td>
                            <asp:Label ID="Label12" runat="server" Text="Type :" SkinID="ContentLabel"></asp:Label>
                        </td>
                        <td>
                            <telerik:RadComboBox ID="rcbTypeSearch" runat="server">
                                <Items>
                                    <telerik:RadComboBoxItem runat="server" Text="-Select-" Value="UNDEFINED" />
                                    <telerik:RadComboBoxItem runat="server" Text="System" Value="SYSTEM" />
                                    <telerik:RadComboBoxItem runat="server" Text="Subsystem" Value="SUBSYSTEM" />
                                    <telerik:RadComboBoxItem runat="server" Text="Equipment" Value="EQUIPMENT" />
                                    <telerik:RadComboBoxItem runat="server" Text="Component" Value="COMPONENT" />
                                    <telerik:RadComboBoxItem runat="server" Text="Subcomponent" Value="SUBCOMPONENT" />
                                </Items>
                            </telerik:RadComboBox>
                        </td>
                    </tr>
                </table>
                <hr />
                <telerik:RadGrid ID="rgSearchResults" runat="server" PageSize="10" AllowPaging="True"
                    AllowMultiRowSelection="true" AutoGenerateColumns="False" CellSpacing="0" GridLines="None">
                    <ClientSettings>
                        <Selecting AllowRowSelect="True" />
                        <ClientEvents OnCommand="rgSearchResults_OnCommand" OnRowDataBound="rgSearchResults_OnRowDataBound"
                            OnGridCreated="rgSearchResults_OnGridCreated" />
                    </ClientSettings>
                    <MasterTableView>
                        <CommandItemSettings ExportToPdfText="Export to PDF" />
                        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
                            <HeaderStyle Width="20px" />
                        </RowIndicatorColumn>
                        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
                            <HeaderStyle Width="20px" />
                        </ExpandCollapseColumn>
                        <Columns>
                            <telerik:GridClientSelectColumn FilterControlAltText="Filter column column" HeaderText="#"
                                UniqueName="column">
                            </telerik:GridClientSelectColumn>
                            <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter column1 column"
                                HeaderText="Name" UniqueName="column1">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Description" FilterControlAltText="Filter column5 column"
                                HeaderText="Description" UniqueName="column5">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Specifications" FilterControlAltText="Filter column2 column"
                                HeaderText="Specifications" UniqueName="column2">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Type" FilterControlAltText="Filter column3 column"
                                HeaderText="Type" UniqueName="column3">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="IsBusinessCritical" FilterControlAltText="Filter column4 column"
                                HeaderText="Is Business Critical" UniqueName="column4">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="IsEnvironmentCritical" FilterControlAltText="Filter column6 column"
                                HeaderText="Is Environment Critical" UniqueName="column6">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="IsSafetyCritical" FilterControlAltText="Filter column7 column"
                                HeaderText="Is Safety Critical" UniqueName="column7">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="IsMandatory" FilterControlAltText="Filter column8 column"
                                HeaderText="Is Mandatory" UniqueName="column8">
                            </telerik:GridBoundColumn>
                        </Columns>
                        <EditFormSettings>
                            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                            </EditColumn>
                        </EditFormSettings>
                    </MasterTableView>
                    <FilterMenu EnableImageSprites="False">
                    </FilterMenu>
                </telerik:RadGrid>
            </div>
        </telerik:RadPageView>
        <telerik:RadPageView ID="RadPageView2" runat="server">
            <div class="ContentTabBody">
                <table>
                    <tr style="vertical-align: top;">
                        <td>
                            <asp:HiddenField ID="hfSystemID" runat="server" />
                            <table>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label1" runat="server" Text="Name :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadTextBox ID="rtbxName" runat="server">
                                        </telerik:RadTextBox>
                                        <uc1:TagItControl ID="TagItControl1" runat="server" />
                                        <asp:HiddenField ID="hfItemID" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top; height: 50px">
                                    <td>
                                        <asp:Label ID="Label2" runat="server" Text="Description :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadTextBox ID="rtbxDiscription" runat="server" TextMode="MultiLine" Height="49px">
                                        </telerik:RadTextBox>
                                    </td>
                                </tr>
                                <tr style="vertical-align: top; height: 50px">
                                    <td>
                                        <asp:Label ID="Label5" runat="server" Text="Specifications :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadTextBox ID="rtbxSpecs" runat="server" TextMode="MultiLine" Height="49px">
                                        </telerik:RadTextBox>
                                    </td>
                                </tr>
                                <tr style="vertical-align: top; height: 50px">
                                    <td>
                                        <asp:Label ID="Label6" runat="server" Text="Synonyms :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadTextBox ID="rtbxSynonyms" runat="server" TextMode="MultiLine" Height="49px">
                                        </telerik:RadTextBox>
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label7" runat="server" Text="Type :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadComboBox ID="rcbTypes" runat="server" OnClientSelectedIndexChanged="rcbTypes_OnClientSelectedIndexChanged">
                                            <Items>
                                                <telerik:RadComboBoxItem runat="server" Text="-Select-" Value="UNDEFINED" />
                                                <telerik:RadComboBoxItem runat="server" Text="System" Value="SYSTEM" />
                                                <telerik:RadComboBoxItem runat="server" Text="Subsystem" Value="SUBSYSTEM" />
                                                <telerik:RadComboBoxItem runat="server" Text="Equipment" Value="EQUIPMENT" />
                                                <telerik:RadComboBoxItem runat="server" Text="Component" Value="COMPONENT" />
                                                <telerik:RadComboBoxItem runat="server" Text="Subcomponent" Value="SUBCOMPONENT" />
                                            </Items>
                                        </telerik:RadComboBox>
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label11" runat="server" Text="Parent :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <telerik:RadComboBox ID="rcbParent" runat="server" DataTextField="Name" DataValueField="ID">
                                        </telerik:RadComboBox>
                                        <asp:HiddenField ID="hfParentID" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label3" runat="server" Text="Is Business Critical :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbIsBusinessCritical" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label13" runat="server" Text="Is Environment Critical :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbIsEnvCritical" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label14" runat="server" Text="Is Safty Critical :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbIsSafetyCritical" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label15" runat="server" Text="Is Mandatory :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbIsMandatory" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label16" runat="server" Text="Maintain Running Hours :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbRunningHours" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label17" runat="server" Text="Is Rotating Equipment :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbRotatingEquip" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label18" runat="server" Text="Maintain Store :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbStore" runat="server" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td>
                                        <asp:Label ID="Label4" runat="server" Text="Is Disabled :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:CheckBox ID="cbIsDisabled" runat="server" />
                                    </td>
                                </tr>                                
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </telerik:RadPageView>
        <telerik:RadPageView ID="RadPageView3" runat="server">
            <div class="ContentTabBody">
                <table>
                    <tr style="vertical-align: top;">
                        <td>
                            <table>
                                <tr style="vertical-align: top">
                                    <td style="width: 150px">
                                        <asp:Label ID="Label8" runat="server" Text="Add Constituents :" SkinID="ContentLabel"></asp:Label>
                                    </td>
                                    <td>
                                        <asp:ImageButton ID="imgBtnAddConstituents" runat="server" ImageAlign="Middle" ImageUrl="~/Images/Add_Plus.png"
                                            OnClientClick="return loadAddConstituentsWindow();" />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td colspan="2">
                                        <asp:Label ID="Label10" runat="server" Text="Current constituents for this item :"
                                            SkinID="ContentHeading"></asp:Label>
                                        <hr />
                                    </td>
                                </tr>
                                <tr style="vertical-align: top">
                                    <td colspan="2">
                                        <telerik:RadGrid ID="rgConstituents" runat="server" PageSize="10" AllowPaging="True"
                                            AutoGenerateColumns="False" CellSpacing="0" GridLines="None">
                                            <ClientSettings>
                                                <Selecting AllowRowSelect="True" />
                                                <ClientEvents OnCommand="rgConstituents_OnCommand" OnRowDataBound="rgConstituents_OnRowDataBound"
                                                    OnGridCreated="rgConstituents_OnGridCreated" />
                                            </ClientSettings>
                                            <MasterTableView>
                                                <CommandItemSettings ExportToPdfText="Export to PDF" />
                                                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column" Visible="True">
                                                    <HeaderStyle Width="20px" />
                                                </RowIndicatorColumn>
                                                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column" Visible="True">
                                                    <HeaderStyle Width="20px" />
                                                </ExpandCollapseColumn>
                                                <Columns>
                                                    <telerik:GridClientSelectColumn FilterControlAltText="Filter column column" HeaderText="#"
                                                        UniqueName="column">
                                                    </telerik:GridClientSelectColumn>
                                                    <telerik:GridBoundColumn FilterControlAltText="Filter column1 column" HeaderText="Name"
                                                        UniqueName="column1" DataField="Name">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Description" FilterControlAltText="Filter column5 column"
                                                        HeaderText="Description" UniqueName="column5">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn FilterControlAltText="Filter column2 column" HeaderText="Specifications"
                                                        UniqueName="column2" DataField="Specifications">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn FilterControlAltText="Filter column3 column" HeaderText="Type"
                                                        UniqueName="column3" DataField="Type">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="IsBusinessCritical" FilterControlAltText="Filter column4 column"
                                                        HeaderText="Is Business Critical" UniqueName="column4">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="IsEnvironmentCritical" FilterControlAltText="Filter column6 column"
                                                        HeaderText="Is Environment Critical" UniqueName="column6">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="IsSafetyCritical" FilterControlAltText="Filter column7 column"
                                                        HeaderText="Is Safety Critical" UniqueName="column7">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="IsMandatory" FilterControlAltText="Filter column8 column"
                                                        HeaderText="Is Mandatory" UniqueName="column8">
                                                    </telerik:GridBoundColumn>
                                                </Columns>
                                                <EditFormSettings>
                                                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                                                    </EditColumn>
                                                </EditFormSettings>
                                            </MasterTableView>
                                            <FilterMenu EnableImageSprites="False">
                                            </FilterMenu>
                                        </telerik:RadGrid>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </div>
        </telerik:RadPageView>
    </telerik:RadMultiPage>
    <telerik:RadWindow ID="rwAddConstituents" runat="server" Modal="true"  Height="418px" Width="600px"
        BackColor="#f5e6f034"  Title="Add Constituents" ShowContentDuringLoad="false"
        Behaviors="Resize, Close, Move, Reload" VisibleStatusbar="false" OnClientClose="rwAddConstituents_OnClientClose">
    </telerik:RadWindow>
    <script type="text/javascript">

        function rcbTypes_OnClientSelectedIndexChanged(sender, args) {
            var type = $find('<%=rcbTypes.ClientID %>');
            var parentID = $get('<%=hfParentID.ClientID %>');
            var parentRCB = $find('<%=rcbParent.ClientID %>');

            parentRCB.trackChanges();
            parentRCB.clearItems();

            if (type.get_selectedIndex() > 1) {
                var lbl = $get('<%=Label11.ClientID %>');
                lbl.innerHTML = 'Parent ' + type.get_items().getItem(type.get_selectedIndex() - 1).get_text() + " :";

                Landscape.AdministrativeActions.GetItemsByType(type.get_items().getItem(type.get_selectedIndex() - 1).get_value(), -1, 0, function (result, e) {

                    parentRCB.enable();


                    var comboItem = new Telerik.Web.UI.RadComboBoxItem();
                    comboItem.set_text("-Select-");
                    comboItem.set_value("00000000-0000-0000-0000-000000000000");
                    parentRCB.get_items().add(comboItem);

                    for (var idx = 0; idx < result.length; idx++) {
                        comboItem = new Telerik.Web.UI.RadComboBoxItem();
                        comboItem.set_text(result[idx].Name);
                        comboItem.set_value(result[idx].ID);
                        parentRCB.get_items().add(comboItem);
                    }

                    if (parentID.value.trim() != '00000000-0000-0000-0000-000000000000')
                        parentRCB.findItemByValue(parentID.value.trim()).select();
                    else
                        parentRCB.get_items().getItem(0).select();

                }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
            }
            else {
                var lbl = $get('<%=Label11.ClientID %>');
                lbl.innerHTML = 'Parent :';
                parentID.value = '00000000-0000-0000-0000-000000000000';

                if (parentRCB.get_items() != null && parentRCB.get_items().getItem(0) != null)
                    parentRCB.get_items().getItem(0).select();

                parentRCB.disable();
            }
            parentRCB.commitChanges();
        }

        function rtbModuleActions_OnClientButtonClicked(sender, args) {
            processCommands(args.get_item().get_commandName());
            args.get_item().set_checked(false);
        }

        function rgSearchResults_OnGridCreated(sender, args) {
            sender.get_masterTableView().set_dataSource('');
            sender.get_masterTableView().dataBind();
        }

        function rgConstituents_OnGridCreated(sender, args) {
            sender.get_masterTableView().set_dataSource('');
            sender.get_masterTableView().dataBind();
        }

        function processCommands(pCmdName) {
            switch (pCmdName) {
                case 'SAVE':
                    {
                        performSave();
                        break;
                    }

                case 'LIST':
                    {
                        performList();
                        break;
                    }

                case 'DELETE':
                    {
                        perfromDelete();
                        break;
                    }

                case 'NEW':
                    {
                        perfromNew();
                        break;
                    }
            }
        }

        function perfromNew() {
            $find('<%=rtbxName.ClientID %>').set_value('')
            $find('<%=rtbxDiscription.ClientID %>').set_value('');
            $find('<%=rtbxSpecs.ClientID %>').set_value('');
            $find('<%=rtbxSynonyms.ClientID %>').set_value('');
            $get('<%=hfItemID.ClientID %>').value = '';
            $find('<%=rcbTypes.ClientID %>').get_items().getItem(0).select();
            $find('<%=rgConstituents.ClientID %>').get_masterTableView().set_dataSource('');
            $find('<%=rgConstituents.ClientID %>').get_masterTableView().dataBind();
            $get('<%=cbIsBusinessCritical.ClientID %>').checked = false;
            $get('<%=cbIsEnvCritical.ClientID %>').checked = false;
            $get('<%=cbIsSafetyCritical.ClientID %>').checked = false;
            $get('<%=cbIsDisabled.ClientID %>').checked = false;
            $get('<%=cbIsMandatory.ClientID %>').checked = false;
            $get('<%=cbRunningHours.ClientID %>').checked = false;
            $get('<%=cbRotatingEquip.ClientID %>').checked = false;
            $get('<%=cbStore.ClientID %>').checked = false;
            var rts = $find('<%=rtsSystemManagement.ClientID %>');
            if (rts.get_selectedTab().get_index() != 1) {
                rts.get_tabs().getTab(1).select();
            }
        }

        function perfromDelete() {
            var rts = $find('<%=rtsSystemManagement.ClientID %>');
            var selectedIDs = '';
            if (rts.get_selectedTab().get_index() == 1) {
                //Current tab is details
                if (confirm('Do you want to delete the selected item?')) {
                    selectedIDs += $get('<%=hfItemID.ClientID %>').value + ';';
                }
            }
            else {//Current tab is search tab
                if (confirm('Do you want to delete the selected items?')) {
                    var radgrid = $find('<%=rgSearchResults.ClientID %>');
                    var selectedItems = radgrid.get_selectedItems();
                    for (var idx = 0; idx < selectedItems.length; idx++) {
                        selectedIDs += selectedItems[idx].get_dataItem().ID + ';';
                    }
                }
            }

            if (selectedIDs != '') {
                Landscape.AdministrativeActions.DeleteItems(selectedIDs, MASTER_GetCurrentUserCode(), function (result, e) {
                    N_NotifyMessage('Success !!!', 'Selected item(s) deleted successfully.', 1);
                    perfromNew(); performList();
                    var radgrid = $find('<%=rgSearchResults.ClientID %>');
                    radgrid.clearSelectedItems();
                }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
            }
        }

        function performSave() {
            var rts = $find('<%=rtsSystemManagement.ClientID %>');
            var name = $find('<%=rtbxName.ClientID %>').get_value();
            var description = $find('<%=rtbxDiscription.ClientID %>').get_value();
            var specification = $find('<%=rtbxSpecs.ClientID %>').get_value();
            var type = $find('<%=rcbTypes.ClientID %>').get_value();
            var synonyms = $find('<%=rtbxSynonyms.ClientID %>').get_value();
            var parentID = $find('<%=rcbParent.ClientID %>').get_value();

            switch (rts.get_selectedTab().get_index()) {
                case 0: //Search Tab
                    {
                        return false;
                        break;
                    }

                case 1: //Details Tab
                    {
                        var item = new Landscape.MachineType();
                        if ($get('<%=hfItemID.ClientID %>').value.trim() != '')
                            item.ID = $get('<%=hfItemID.ClientID %>').value.trim();
                        else
                            item.CreatedBy = MASTER_GetCurrentUserCode();

                        item.Name = name;
                        item.Description = description;
                        item.Specifications = specification;
                        item.Type = type;
                        item.IsBusinessCritical = $get('<%=cbIsBusinessCritical.ClientID %>').checked;
                        item.IsEnvironmentCritical = $get('<%=cbIsEnvCritical.ClientID %>').checked;
                        item.IsSafetyCritical = $get('<%=cbIsSafetyCritical.ClientID %>').checked;
                        item.IsDisabled = $get('<%=cbIsDisabled.ClientID %>').checked;
                        item.IsMandatory = $get('<%=cbIsMandatory.ClientID %>').checked;
                        item.RunningHoursRequred = $get('<%=cbRunningHours.ClientID %>').checked;
                        item.IsRotatingEquipment = $get('<%=cbRotatingEquip.ClientID %>').checked;
                        item.IsStoreRequired = $get('<%=cbStore.ClientID %>').checked;

                        if (synonyms.trim() != '') {
                            var synArray = new Array();
                            for (var idx = 0; idx < synonyms.split(';').length; idx++) {
                                synArray.push(synonyms.split(';')[idx]);
                            }

                            item.Synonyms = synArray;
                        }
                        if (parentID == '')
                            parentID = null;
                        item.ParentID = parentID;
                        item.UpdatedBy = MASTER_GetCurrentUserCode();
                        Landscape.AdministrativeActions.SaveItem(item, function (result, e) {
                            $get('<%=hfItemID.ClientID %>').value = result;
                            N_NotifyMessage('Success !!!', 'The item details saved successfully.', 1);
                        },
                        function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
                        break;
                    }
            }

            return false;
        }

        function performList() {
            var rts = $find('<%=rtsSystemManagement.ClientID %>');
            if (rts.get_selectedTab().get_index() != 0) {
                rts.get_tabs().getTab(0).select();
            }

            $find('<%=rgSearchResults.ClientID %>').clearSelectedItems();

            var radgrid = $find('<%=rgSearchResults.ClientID %>').get_masterTableView();
            var name = $find('<%=rtbxNameSearch.ClientID %>').get_value();
            var type = $find('<%=rcbTypeSearch.ClientID %>').get_value();
            if (type == 'UNDEFINED')
                type = '';

            Landscape.AdministrativeActions.GetItemsBy(name.trim(), type.trim(), radgrid.get_currentPageIndex(), radgrid.get_pageSize(), function (result, e) {
                //Bind to radgrid                
                radgrid.set_dataSource(result);
                radgrid.dataBind();
            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });

            Landscape.AdministrativeActions.GetItemsCountBy(name.trim(), type.trim(), function (result, e) {
                radgrid.set_virtualItemCount(result);
            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
        }

        function rgSearchResults_OnCommand(sender, args) {
            switch (args.get_commandName()) {
                case 'PageSize':
                case 'Page':
                    {
                        performList();
                        break;
                    }
            }
            args.set_cancel(true);
        }

        function rgSearchResults_OnRowDataBound(sender, args) {
            var cell = args.get_item().get_cell('column1');
            var anchor = String.format("<a href='#' onclick='return loadItemData({0});'>{1}</a>", String.format("\"{0}\"", args.get_dataItem().ID), args.get_dataItem().Name);
            cell.innerHTML = anchor;

            var cell2 = args.get_item().get_cell('column4');
            cell2.innerHTML = (cell2.innerHTML == 'true' ? 'Yes' : 'No');

            cell2 = args.get_item().get_cell('column6');
            cell2.innerHTML = (cell2.innerHTML == 'true' ? 'Yes' : 'No');

            cell2 = args.get_item().get_cell('column7');
            cell2.innerHTML = (cell2.innerHTML == 'true' ? 'Yes' : 'No');

            cell2 = args.get_item().get_cell('column8');
            cell2.innerHTML = (cell2.innerHTML == 'true' ? 'Yes' : 'No');
        }

        function loadItemData(pItemID) {
            perfromNew();
            Landscape.AdministrativeActions.GetItemByID(pItemID, function (result, e) {
                var name = $find('<%=rtbxName.ClientID %>');
                var description = $find('<%=rtbxDiscription.ClientID %>');
                var specification = $find('<%=rtbxSpecs.ClientID %>');
                var type = $find('<%=rcbTypes.ClientID %>');
                var synonyms = $find('<%=rtbxSynonyms.ClientID %>');
                var parentID = $get('<%=hfParentID.ClientID %>');

                var ts = $find('<%=rtsSystemManagement.ClientID %>');
                ts.get_tabs().getTab(1).select();

                name.set_value(result.Name);
                description.set_value(result.Description);
                specification.set_value(result.Specifications);
                type.findItemByValue(result.Type).select();
                var syns = '';
                if (result.Synonyms != null && result.Synonyms.length > 0) {
                    for (var idx = 0; idx < result.Synonyms.length; idx++) {
                        syns += result.Synonyms[idx] + ';';
                    }
                }
                synonyms.set_value(syns);

                if (result.ParentID > 0)
                    parentID.value = result.ParentID;
                else
                    parentID.value = '00000000-0000-0000-0000-000000000000';

                $get('<%=cbIsBusinessCritical.ClientID %>').checked = result.IsBusinessCritical;
                $get('<%=cbIsEnvCritical.ClientID %>').checked = result.IsEnvironmentCritical;
                $get('<%=cbIsSafetyCritical.ClientID %>').checked = result.IsSafetyCritical;
                $get('<%=cbIsDisabled.ClientID %>').checked = result.IsDisabled;
                $get('<%=hfItemID.ClientID %>').value = result.ID;
                $get('<%=cbIsMandatory.ClientID %>').checked = result.IsMandatory;
                $get('<%=cbRunningHours.ClientID %>').checked = result.RunningHoursRequred;
                $get('<%=cbRotatingEquip.ClientID %>').checked = result.IsRotatingEquipment;
                $get('<%=cbStore.ClientID %>').checked = result.IsStoreRequired;

                loadConstituentsByParentID(result.ID);

            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
            return false;
        }

        function rgConstituents_OnCommand(sender, args) {
            switch (args.get_commandName()) {
                case 'PageSize':
                case 'Page':
                    {
                        loadConstituentsByParentID($get('<%=hfItemID.ClientID %>').value);
                        break;
                    }
            }
            args.set_cancel(true);
        }

        function loadConstituentsByParentID(pID) {
            var rgConstituents = $find('<%=rgConstituents.ClientID %>').get_masterTableView();
            Landscape.AdministrativeActions.GetConstituentsByParentID(pID, rgConstituents.get_currentPageIndex(), rgConstituents.get_pageSize(), function (result, e) {
                rgConstituents.set_dataSource(result);
                rgConstituents.dataBind();
            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });

            Landscape.AdministrativeActions.GetConstituentsCountByParentID(pID, function (result, e) {
                rgConstituents.set_virtualItemCount(result);
            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
        }

        function rgConstituents_OnRowDataBound(sender, args) {
            var cell = args.get_item().get_cell('column1');
            var anchor = String.format("<a href='#' onclick='return loadItemData({0});'>{1}</a>", String.format("\"{0}\"", args.get_dataItem().ID), args.get_dataItem().Name);
            cell.innerHTML = anchor;

            var cell2 = args.get_item().get_cell('column4');
            cell2.innerHTML = (cell2.innerHTML == 'true' ? 'Yes' : 'No');

            cell2 = args.get_item().get_cell('column6');
            cell2.innerHTML = (cell2.innerHTML == 'true' ? 'Yes' : 'No');

            cell2 = args.get_item().get_cell('column7');
            cell2.innerHTML = (cell2.innerHTML == 'true' ? 'Yes' : 'No');

            cell2 = args.get_item().get_cell('column8');
            cell2.innerHTML = (cell2.innerHTML == 'true' ? 'Yes' : 'No');
        }

        function loadAddConstituentsWindow() {
            if ($get('<%=hfItemID.ClientID %>').value.trim() != '') {
                var type = $find('<%=rcbTypes.ClientID %>');
                var url = '<%=Page.ResolveClientUrl("~/Internal/InternalWindows.aspx") %>';
                url += '?ctrl=ADDCON&PID=' + $get('<%=hfItemID.ClientID %>').value + '&T=' + type.get_items().getItem(type.get_selectedIndex() + 1).get_value();

                $find('<%=rwAddConstituents.ClientID %>').setUrl(url);
                $find('<%=rwAddConstituents.ClientID %>').show();
            }
            else {
                N_NotifyMessage('Parent not found !!!', 'Please select a parent or create a parent to add its constituents.', 3);
            }
            return false;
        }

        function rwAddConstituents_OnClientClose(sender, args) {
            Landscape.AdministrativeActions.GetConstituentsByParentID($get('<%=hfItemID.ClientID %>').value, 0, 10, function (result, e) {
                var rgConstituents = $find('<%=rgConstituents.ClientID %>').get_masterTableView();
                rgConstituents.set_dataSource(result);
                rgConstituents.dataBind();
            }, function (result, e) { N_NotifyMessage('Error !!!', result.get_message(), 2); });
        }

    </script>
</asp:Content>
